<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

class OwnserviceHelper
{
	var $total_amount 		= null;

	var $total_nextamount 	= null;

	var $max_current 		= null;

	/**
	 * Method to build the list branches
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildbranchesselect($list, $name, $selected, $top, $class = 'class="inputbox"'){
		$branchlist 	= array();
		
		if($top) {
			$branchlist[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_BRANCH' ) );
		}
		
		foreach ($list as $item) {
			$branchlist[] = JHTML::_( 'select.option', $item->id, $item->name);
		}
		return JHTML::_('select.genericlist', $branchlist, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method to build the list for users
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildusersselect($list, $name, $selected, $top, $class = 'class="inputbox"'){
		
		$userlist 	= array();
		
		if($top) {
			$userlist[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_USER' ) );
		}
		
		foreach ($list as $item) {
			$userlist[] = JHTML::_( 'select.option', $item->id, $item->username.' '.$item->name);
		}

		return JHTML::_('select.genericlist', $userlist, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method to build the list for bank account
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildbanksselect($list, $name, $selected, $top, $class = 'class="inputbox"'){
		
		$banklist 	= array();
		
		if($top) {
			$banklist[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_ACCOUNT' ) );
		}
		
		if($list) {
			foreach ($list as $item) {
				$banklist[] = JHTML::_( 'select.option', $item->account_no, OwnserviceHelper::accountFormat($item->account_no).' | '.$item->account_name.' | '.$item->bank_name);
			}
		}
		return JHTML::_('select.genericlist', $banklist, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method to build the list for countries
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildcountryselect($list, $name, $selected, $top, $class = 'class="inputbox"'){
		
		$countrylist 	= array();
		
		if($top) {
			$countrylist[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_COUNTRY' ) );
		}
		
		if($list) {
			foreach ($list as $item) {
				$countrylist[] = JHTML::_( 'select.option', $item->country, $item->country);
			}
		}
		return JHTML::_('select.genericlist', $countrylist, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method to build the list for currencies
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildcurrencyselect($list, $name, $selected, $top, $class = 'class="inputbox"'){
		
		$currencylist 	= array();
		
		if($top) {
			$currencylist[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_CURRENCY' ) );
		}
		
		if($list) {
			foreach ($list as $item) {
				$currencylist[] = JHTML::_( 'select.option', $item->code, $item->name);
			}
		}
		return JHTML::_('select.genericlist', $currencylist, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method to build the list for groups when performing an edit action
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildgroupsselect($list, $name, $selected, $top, $class = 'class="inputbox"'){
		$grouplist 	= array();
		
		if($top) {
			$grouplist[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_ACCESS_LEVEL' ) );
		}
		
		foreach ($list as $item) {
			$grouplist[] = JHTML::_( 'select.option', $item->id, $item->name);
		}
		return JHTML::_('select.genericlist', $grouplist, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method to build the list for receiving
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildreceivingselect($list, $name, $selected, $top, $class = 'class="inputbox"') {
		$receivinglist 	= array();
		if($top) {
			$receivinglist[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_STAFF_TRANSFER' ) );
		}
		foreach ($list as $item) {
			$receivinglist[] = JHTML::_( 'select.option', $item->id, $item->name);
		}
		return JHTML::_('select.genericlist', $receivinglist, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method to build the list bank account
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildaccounttypeselect($name, $selected, $top, $class = 'class="inputbox"'){
		
		$account_type = array();
		
		if($top) {
			$account_type[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_ACCOUNT_TYPE' ) );
		}

		$account_type[]		= JHTML::_( 'select.option', 'savings', JText::_('OWNSERVICE_SAVING'));
		$account_type[]		= JHTML::_( 'select.option', 'current', JText::_('OWNSERVICE_CIRCULATE'));
		$account_type[]		= JHTML::_( 'select.option', 'term deposit', JText::_('OWNSERVICE_DEPOSITS'));
		$account_type[]		= JHTML::_( 'select.option', 'credit card', JText::_('OWNSERVICE_CREDIT_CARD'));
		$account_type[]		= JHTML::_( 'select.option', 'other', JText::_('OWNSERVICE_OTHER'));
		
		return JHTML::_('select.genericlist', $account_type, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method get bank list
	 *
	 * @return array
	 * @since 1.5
	*/
	function buildbanknamelist($name, $selected, $top, $class = 'class="inputbox"')
	{
		$options = array();

		if($top) {
			$options[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_BANK' ) );
		}
		$options[]		= JHTML::_( 'select.option', 'Bangkok Bank', JText::_('Bangkok Bank'));
		$options[]		= JHTML::_( 'select.option', 'Bank of Ayudhya', JText::_('Bank of Ayudhya'));
		$options[]		= JHTML::_( 'select.option', 'Krung Thai Bank', JText::_('Krung Thai Bank'));
		$options[]		= JHTML::_( 'select.option', 'Siam Commercial Bank', JText::_('Siam Commercial Bank'));
		$options[]		= JHTML::_( 'select.option', 'Kasikorn Bank', JText::_('Kasikorn Bank'));
		$options[]		= JHTML::_( 'select.option', 'Thai Military Bank', JText::_('Thai Military Bank'));
		$options[]		= JHTML::_( 'select.option', 'United Overseas Bank', JText::_('United Overseas Bank'));
		$options[]		= JHTML::_( 'select.option', 'TISCO', JText::_('TISCO'));
		$options[]		= JHTML::_( 'select.option', 'Tanachart Bank', JText::_('Tanachart Bank'));
		$options[]		= JHTML::_( 'select.option', 'Government Savings Bank', JText::_('Government Savings Bank'));
		$options[]		= JHTML::_( 'select.option', 'Government Housing Bank', JText::_('Government Housing Bank'));
		$options[]		= JHTML::_( 'select.option', 'Bank for Agriculture', JText::_('Bank for Agriculture'));
		$options[]		= JHTML::_( 'select.option', 'Standard Chatered Bank', JText::_('Standard Chatered Bank'));
		$options[]		= JHTML::_( 'select.option', 'CIMB', JText::_('CIMB'));
		$options[]		= JHTML::_( 'select.option', 'other', JText::_('OWNSERVICE_OTHER'));

		return JHTML::_('select.genericlist', $options, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method to build the list ID Type
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildidtypeselect($name, $selected, $top, $class = 'class="inputbox"'){
		
		$idtypelist = array();
		
		if($top) {
			$idtypelist[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_ID_TYPE' ) );
		}

		$idtypelist[]		= JHTML::_( 'select.option', 'Passport', JText::_('OWNSERVICE_PASSPORT'));
		$idtypelist[]		= JHTML::_( 'select.option', 'Photo ID', JText::_('OWNSERVICE_PHOTO_ID'));
		$idtypelist[]		= JHTML::_( 'select.option', "Driver's licence", JText::_('OWNSERVICE_DRIVER_LICENCE'));
		
		return JHTML::_('select.genericlist', $idtypelist, $name, $class, 'value', 'text', $selected );

	}
	/**
	 * Method to build the list ID Type
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildissuerselect($name, $selected, $top, $class = 'class="inputbox"'){
		
		$issuerlist = array();
		
		if($top) {
			$issuerlist[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_ISSUER' ) );
		}

		$issuerlist[]		= JHTML::_( 'select.option', 'Ministry of Foreign Affairs', JText::_('OWNSERVICE_MINISTRY_AFFAIRS'));
		$issuerlist[]		= JHTML::_( 'select.option', 'RTA', JText::_('OWNSERVICE_RTA'));
		$issuerlist[]		= JHTML::_( 'select.option', 'Australia', JText::_('Australia') );
		$issuerlist[]		= JHTML::_( 'select.option', 'Other',  JText::_('OWNSERVICE_OTHER'));
		
		return JHTML::_('select.genericlist', $issuerlist, $name, $class, 'value', 'text', $selected );

	}

	/**
	 * Method to build the list currencies
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildcurrenciesselect($list, $name, $selected, $top, $class = 'class="inputbox"'){
		
		$currencylist = array();
		
		if($top) {
			$currencylist[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_CURRENCY' ) );
		}

		foreach ($list as $item) {
			$currencylist[] = JHTML::_( 'select.option', $item->id, $item->name);
		}
		return JHTML::_('select.genericlist', $currencylist, $name, $class, 'value', 'text', $selected );
	}

	/**
	* Method to build the reasons select list
	*
	* @return array
	* @since 1.5
	*/
	function buildreasonsselect($name, $selected='GIFT', $top, $class = 'class="inputbox"') {
		$reasonlist = array();

		if($top) {
			$reasonlist[] = JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_REASON' ) );
		}

		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('GIFT')),		ucwords(strtolower(JText::_('OWNSERVICE_GIFT'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Monthly Expenses')),		ucwords(strtolower(JText::_('OWNSERVICE_MONTHLY_EXPENSES'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Electricity Bill')),		ucwords(strtolower(JText::_('OWNSERVICE_ELECTRICITY_BILL'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Water bill')),		ucwords(strtolower(JText::_('OWNSERVICE_WATER_BILL'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('RENT')),		ucwords(strtolower(JText::_('OWNSERVICE_RENT'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('SHOPPING')),		ucwords(strtolower(JText::_('OWNSERVICE_SHOPPING'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Personal Savings')),		ucwords(strtolower(JText::_('OWNSERVICE_PERSONAL_SAVINGS'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('AIR FARE')),		ucwords(strtolower(JText::_('OWNSERVICE_AIR_FARE'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('TRAVEL')),		ucwords(strtolower(JText::_('OWNSERVICE_TRAVEL'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('MEDICAL')),		ucwords(strtolower(JText::_('OWNSERVICE_MEDICAL'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('EDUCATION')),		ucwords(strtolower(JText::_('OWNSERVICE_EDUCATION'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Car Installment')),		ucwords(strtolower(JText::_('OWNSERVICE_CAR_INSTALLMENT'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Car Loan Repayment')),		ucwords(strtolower(JText::_('OWNSERVICE_CAR_LOAN_REPAYMENT'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('House Intallment')),		ucwords(strtolower(JText::_('OWNSERVICE_HOUSE_INTALLMENT'))));
		$reasonlist[] = JHTML::_( 'select.option', ucwords(strtolower('Home Loan Repayment')),		ucwords(strtolower(JText::_('OWNSERVICE_HOME_LOAN_REPAYMENT'))));
		$reasonlist[] = JHTML::_( 'select.option', 'other', JText::_('OWNSERVICE_OTHER'));

		return JHTML::_('select.genericlist', $reasonlist, $name, $class, 'value', 'text', $selected );
	}

	/**
	* Method to build the time transfer select list
	*
	* @return array
	* @since 1.5
	*/
	function buildtimetransferselect($name, $selected='', $top, $class = 'class="inputbox"') {
		$timelist = array();

		if($top) {
			$timelist[] = JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_TIME_TRANSFER' ) );
		}
		$timelist[] = JHTML::_( 'select.option', 'morning',	JText::_('Morning'));
		$timelist[] = JHTML::_( 'select.option', 'evening',	JText::_('Evening'));
		$timelist[] = JHTML::_( 'select.option', 'night',		JText::_('Night'));

		return JHTML::_('select.genericlist', $timelist, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method to build the filter list status
	 *
	 * @return array
	 * @since 1.5
	*/
	function buildfilterstatuslist($name='filter_status', $selected, $top, $class = 'class="inputbox"'){
		$statuslist = array();

		if($top){
			$statuslist[] = JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_STATUS' ) );
		}

		/*$statuslist[] = JHTML::_( 'select.option', 'C',		JText::_('OWNSERVICE_CANCEL'));
		$statuslist[] = JHTML::_( 'select.option', 'F',		JText::_('OWNSERVICE_FAIL'));
		$statuslist[] = JHTML::_( 'select.option', 'W',		JText::_('OWNSERVICE_FOR_PAYMENT'));
		$statuslist[] = JHTML::_( 'select.option', 'P',	JText::_('OWNSERVICE_PAID'));
		$statuslist[] = JHTML::_( 'select.option', 'AP',	JText::_('OWNSERVICE_APPROVAL_PENDING'));
		$statuslist[] = JHTML::_( 'select.option', 'AT',	JText::_('OWNSERVICE_TRANSFER_PENDING'));
		$statuslist[] = JHTML::_( 'select.option', 'A',		JText::_('OWNSERVICE_APPROVED'));
		$statuslist[] = JHTML::_( 'select.option', 'S',		JText::_('OWNSERVICE_SUCCESS'));*/

		$statuslist[] = JHTML::_( 'select.option', '1',		JText::_('OWNSERVICE_PAID'));
		$statuslist[] = JHTML::_( 'select.option', '2',		JText::_('OWNSERVICE_APPROVAL_PENDING'));
		$statuslist[] = JHTML::_( 'select.option', '3',		JText::_('OWNSERVICE_TRANSFERRING'));
		$statuslist[] = JHTML::_( 'select.option', '4',		JText::_('OWNSERVICE_SUCCESSFUL'));
		$statuslist[] = JHTML::_( 'select.option', '5',		JText::_('OWNSERVICE_FAIL'));
		$statuslist[] = JHTML::_( 'select.option', '6',		JText::_('OWNSERVICE_CORRECTED'));
		$statuslist[] = JHTML::_( 'select.option', '7',		JText::_('OWNSERVICE_APPROVAL_PENDING_CORRECTED'));

		return JHTML::_('select.genericlist', $statuslist, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method to build the list status of transaction
	 *
	 * @return array
	 * @since 1.5
	*/
	function buildstatusselect($name, $selected, $top, $class = 'class="inputbox"'){

		$statuslist = array();

		if($top){
			$statuslist[] = JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_STATUS' ) );
		}

		/**
		 * Old status
		 * -4	= Suspended Transfer
		 * -3	= Fixed
		 * -2	= Cancel
		 * -1	= Fail
		 * 0	= Waiting for payment
		 * 1	= Paid (in-process)
		 * 2	= Requested Approval
		 * 3	= Transfer Pending
		 * 4	= Approved
		 * 5	= Success
		*/

		/**
		 * New status
		 * 
		 * 1 = Paid
		 * 2 = Approval Pending
		 * 3 = Transferring
		 * 4 = Successful
		 * 5 = Fail
		 * 6 = Corrected
		 * 7 = Approval Pending (Corrected)
		 * 8 = Cancel
		*/

		//Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		//get vars
		$gid		= $user->gid;
		$level		= $ownuser->get('level_id');

		if( $gid < 25 )
		{
			switch ($level)
			{
				case "1":
					if($selected == 'C')
						return JText::_('OWNSERVICE_CANCEL');
					else if($selected == 'F')
						return JText::_('OWNSERVICE_FAIL');
					else if($selected == 'P')
						return JText::_('OWNSERVICE_PAID');
					else if($selected == 'AP')
						return JText::_('OWNSERVICE_APPROVAL_PENDING');
					else if($selected == 'AT')
						return JText::_('OWNSERVICE_TRANSFER_PENDING');
					else if($selected == 'A')
						return JText::_('OWNSERVICE_APPROVED');
					else if($selected == 'S')
						return JText::_('OWNSERVICE_SUCCESS');

					$statuslist[] = JHTML::_( 'select.option','W', JText::_('OWNSERVICE_FOR_PAYMENT'));
				break;
				case "2":
					if($selected == 'P')
						return JText::_('OWNSERVICE_PAID').' <input type="hidden" name="state" id="state" value="P" />'; 
					else if($selected == 'AP')
						return JText::_('OWNSERVICE_APPROVAL_PENDING').' <input type="hidden" name="state" id="state" value="AP" />';
					else if($selected == 'AT')
						return JText::_('OWNSERVICE_TRANSFER_PENDING').' <input type="hidden" name="state" id="state" value="AT" />';
					else if($selected == 'A')
						return JText::_('OWNSERVICE_APPROVED').' <input type="hidden" name="state" id="state" value="A" />';
					else if($selected == 'S')
						return JText::_('OWNSERVICE_SUCCESS').' <input type="hidden" name="state" id="state" value="S" />';

					$statuslist[] = JHTML::_( 'select.option','C',		JText::_('OWNSERVICE_CANCEL'));
					$statuslist[] = JHTML::_( 'select.option','W',		JText::_('OWNSERVICE_FOR_PAYMENT'));
					$statuslist[] = JHTML::_( 'select.option','P',		JText::_('OWNSERVICE_PAID'));
				break;
				case "3":
					$statuslist[] = JHTML::_( 'select.option', 'F',		JText::_('OWNSERVICE_FAIL'));
					$statuslist[] = JHTML::_( 'select.option', 'AT',	JText::_('OWNSERVICE_TRANSFER_PENDING'));
					$statuslist[] = JHTML::_( 'select.option', 'S',		JText::_('OWNSERVICE_SUCCESS'));
				break;
				case "4":
				case "5":
					if($selected == 'AT')
						return JText::_('OWNSERVICE_TRANSFER_PENDING').' <input type="hidden" name="state" id="state" value="AT" />';
					else if($selected == 'A')
						return JText::_('OWNSERVICE_APPROVED').' <input type="hidden" name="state" id="state" value="A" />';
					else if($selected == 'S')
						return JText::_('OWNSERVICE_SUCCESS').' <input type="hidden" name="state" id="state" value="S" />';
					
					$statuslist[] = JHTML::_( 'select.option','C',		JText::_('OWNSERVICE_CANCEL'));
					$statuslist[] = JHTML::_( 'select.option','W',		JText::_('OWNSERVICE_FOR_PAYMENT'));
					$statuslist[] = JHTML::_( 'select.option','P',		JText::_('OWNSERVICE_PAID'));
					$statuslist[] = JHTML::_( 'select.option', 'AP',	JText::_('OWNSERVICE_APPROVAL_PENDING'));
				break;
				case "6":
					$statuslist[] = JHTML::_( 'select.option', 'C',		JText::_('OWNSERVICE_CANCEL'));
					$statuslist[] = JHTML::_( 'select.option', 'F',		JText::_('OWNSERVICE_FAIL'));
					$statuslist[] = JHTML::_( 'select.option', 'W',		JText::_('OWNSERVICE_FOR_PAYMENT'));
					$statuslist[] = JHTML::_( 'select.option', 'P',	JText::_('OWNSERVICE_PAID'));
					$statuslist[] = JHTML::_( 'select.option', 'AP',	JText::_('OWNSERVICE_APPROVAL_PENDING'));
					$statuslist[] = JHTML::_( 'select.option', 'AT',	JText::_('OWNSERVICE_TRANSFER_PENDING'));
					$statuslist[] = JHTML::_( 'select.option', 'A',		JText::_('OWNSERVICE_APPROVED'));
					$statuslist[] = JHTML::_( 'select.option', 'S',		JText::_('OWNSERVICE_SUCCESS'));
				break;
			}
		} else {
				$statuslist[] = JHTML::_( 'select.option', 'C',		JText::_('OWNSERVICE_CANCEL'));
				$statuslist[] = JHTML::_( 'select.option', 'F',		JText::_('OWNSERVICE_FAIL'));
				$statuslist[] = JHTML::_( 'select.option', 'W',		JText::_('OWNSERVICE_FOR_PAYMENT'));
				$statuslist[] = JHTML::_( 'select.option', 'P',	JText::_('OWNSERVICE_PAID'));
				$statuslist[] = JHTML::_( 'select.option', 'AP',	JText::_('OWNSERVICE_APPROVAL_PENDING'));
				$statuslist[] = JHTML::_( 'select.option', 'AT',	JText::_('OWNSERVICE_TRANSFER_PENDING'));
				$statuslist[] = JHTML::_( 'select.option', 'A',		JText::_('OWNSERVICE_APPROVED'));
				$statuslist[] = JHTML::_( 'select.option', 'S',		JText::_('OWNSERVICE_SUCCESS'));
		}


		return JHTML::_('select.genericlist', $statuslist, $name, $class, 'value', 'text', $selected );
	}

	/**
	 * Method to get account type
	 *
	 * @return string
	 * @since 1.5
	*/
	function getAccountType($type='savings') {
		$text = '';
		switch($type):
			case "savings":
				$text = JText::_('OWNSERVICE_SAVING');
			break;
			case "current":
				$text = JText::_('OWNSERVICE_CIRCULATE');
			break;
			case "term deposit":
				$text = JText::_('OWNSERVICE_DEPOSITS');
			break;
			case "credit card":
				$text = JText::_('OWNSERVICE_CREDIT_CARD');
			break;
		endswitch;
		return $text;
	}

	/**
	 * Method get format price
	 * @return string
	*/
	function moneyFormat($format,$data){
		switch($format){
			case '#,###.##':
				return number_format ( $data ,2,".",",");
			break;
			case '#.###.##':
				return number_format ( $data ,2,".",".");
			break;
			case '#,###':
				return number_format ( $data ,0,".",",");
			break;
			case '#.###':
				return number_format ( $data ,0,".",".");
			break;
			case '####':
				return number_format ( $data ,0,".","");
			break;
		}
			return number_format ( $data ,0,".","");
	}

	function transactionFormat($transectionid, $branchname, $transectiondate){

		$arrBranch = str_split($branchname);
		$arrDate = explode(' ',$transectiondate);
		$arrYear = str_split($arrDate[0],2);
		$date = explode('-', $arrDate[0]);
		return strtoupper($arrBranch[0].$date[1].$date[2].$transectionid);

	}

	function memberFormat($userid, $name, $birthday) {
	
		$arrName = str_split(htmlspecialchars($name, ENT_QUOTES, 'UTF-8'), 2);
		list($year,$month, $day) = split('[/.-]', $birthday);
		//edit date 30/09/2554
		//return strtoupper($arrName[0]).$day.$month.$userid;
		$arrYear = str_split($year, 2);
		return strtoupper($arrName[0]).$day.$month.$arrYear[1];

	}

	/**
	 * Method to get username format
	 * $firstname is param firstname of user
	 * $lastname is param lastname of user
	 * $birthday is param birthday of user
	 * @return string
	 *
	*/
	function usernameFormat($firstname, $lastname, $birthday)
	{
		$fArray = str_split(htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8'), 2);
		$lArray = str_split(htmlspecialchars($lastname, ENT_QUOTES, 'UTF-8'), 2);

		list($year,$month, $day) = split('[/.-]', $birthday);

		return trim(strtoupper($fArray[0])).strtoupper(trim($lArray[0])).trim($day).trim($month);
	}

	/**
	 * Method to get format transfer to account number
	 *
	 * @return string;
	*/
	function accountFormat($account) {
		return $account;
		$num = str_split($account);
		return $num[0].$num[1].$num[2].'-'.$num[3].'-'.$num[4].$num[5].$num[6].$num[7].$num[8].'-'.$num[9];
	}

	/*function getStatus($id, $state){

		$user = & JFactory::getUser();
		$ouser = Ownservice::getUser();
		
		$access_level = $ouser->get('level_name');

		$status = '';
		if( $user->gid < 25 ) {
			switch (strtolower($access_level)) {
					case "member":
							if ( $state == -2 ){
								$status =  JText::_('OWNSERVICE_CANCEL');
							} else if( $state == -1 ) {
								$status =  JText::_('OWNSERVICE_FAIL');
							} else if( $state == 0 ) {
								$status =  JText::_('OWNSERVICE_FOR_PAYMENT');
							} else if( $state == 1 ) {
								$status =  JText::_('OWNSERVICE_PAID'); 
							} else if( $state == 2 ) {
								$status =  JText::_('OWNSERVICE_PAID');
							} else if( $state == 3 ) {
								$status =  JText::_('OWNSERVICE_PAID');
							} else if( $state == 4 ) {
									$status =  JText::_('OWNSERVICE_PAID');
							} else if( $state == 5 ) {
								$status =  JText::_('OWNSERVICE_SUCCESSFUL');
							}
					break;
					case "branch staff":
					case "branch manager":
					case "centre manager":
					case "md":
							if( $state == 0 ){
									/*$status = '
											<select name="state" id="state" class="inputbox" onChange="changeStatus('.$id.',this.value);">
												<option value="W" selected>'.JText::_('OWNSERVICE_FOR_PAYMENT').'</option>
												<option value="P">'.JText::_('OWNSERVICE_PAID').'</option>
												<option value="C">'.JText::_('OWNSERVICE_CANCEL').'</option>
											</select>
										';*/
									/*echo JText::_('OWNSERVICE_FOR_PAYMENT');
							}else {
								if ( $state == -2 ){
									echo JText::_('OWNSERVICE_CANCEL');
								} else if( $state == -1 ) {
									echo JText::_('OWNSERVICE_FAIL');
								} else if( $state == 0 ) {
									echo JText::_('OWNSERVICE_FOR_PAYMENT');
								} else if( $state == 1 ) {
									echo JText::_('OWNSERVICE_PAID'); 
								} else if( $state == 2 ) {
									echo JText::_('OWNSERVICE_APPROVAL_PENDING');
								} else if( $state == 3 ) {
									echo JText::_('OWNSERVICE_TRANSFER_PENDING');
								} else if( $state == 4 ) {
									echo JText::_('OWNSERVICE_APPROVED');
								} else if( $state == 5 ) {
									echo JText::_('OWNSERVICE_SUCCESSFUL');
								}
							}
					break;
					case "transferred staff":
							if( $state == 3 ){
								$status = '
											<div id="chState'.$id.'">
												<select name="state" id="state" class="inputbox" onChange="changeStatus('.$id.',this.value);">
													<option value="AT" selected>'.JText::_('OWNSERVICE_TRANSFER_PENDING').'</option>
													<option value="S">'.JText::_('OWNSERVICE_SUCCESSFUL').'</option>
													<option value="F">'.JText::_('OWNSERVICE_FAIL').'</option>
												</select>
											</div>
										';
							}
					break;
			}
		} else {
				if( $state == 0 ){
					$status = '
								<select name="state" id="state" class="inputbox" onChange="changeStatus('.$id.',this.value);">
									<option value="W" selected>'.JText::_('OWNSERVICE_FOR_PAYMENT').'</option>
									<option value="P">'.JText::_('OWNSERVICE_PAID').'</option>
									<option value="C">'.JText::_('OWNSERVICE_CANCEL').'</option>
								</select>
							';
				}else {
					if ( $state == -2 ){
						echo JText::_('OWNSERVICE_CANCEL');
					} else if( $state == -1 ) {
						echo JText::_('OWNSERVICE_FAIL');
					} else if( $state == 0 ) {
						echo JText::_('OWNSERVICE_FOR_PAYMENT');
					} else if( $state == 1 ) {
						echo JText::_('OWNSERVICE_PAID'); 
					} else if( $state == 2 ) {
						echo JText::_('OWNSERVICE_APPROVAL_PENDING');
					} else if( $state == 3 ) {
						echo JText::_('OWNSERVICE_TRANSFER_PENDING');
					} else if( $state == 4 ) {
						echo JText::_('OWNSERVICE_APPROVED');
					} else if( $state == 5 ) {
						echo JText::_('OWNSERVICE_SUCCESSFUL');
					}
				}
		}

		return $status;
	}*/

	function getState($state)
	{
		switch($state):
			case 1:
				$status = JText::_('OWNSERVICE_PAID');
			break;
			case 2:
				$status = JText::_('OWNSERVICE_APPROVAL_PENDING');
			break;
			case 3:
				$status = JText::_('OWNSERVICE_TRANSFERRING');
			break;
			case 4:
				$status = JText::_('OWNSERVICE_SUCCESSFUL');
			break;
			case 5:
				$status = JText::_('OWNSERVICE_FAIL');
			break;
			case 6:
				$status = JText::_('OWNSERVICE_CORRECTED');
			break;
			case 7:
				$status = JText::_('OWNSERVICE_APPROVAL_PENDING_CORRECTED');
			break;
			case 8:
				$status = JText::_('OWNSERVICE_CANCELLED');
			break;
		endswitch;
		
		return $status;	
	}

	/**
	 * Method to get reason
	 * 
	 * @return string;
	 * @since 1.0
	 */
	function getReason($reason)
	{
		switch(strtoupper($reason)):
			case "GIFT":
				$txt = JText::_('OWNSERVICE_GIFT');
			break;
			case "MONTHLY EXPENSES":
				$txt = JText::_('OWNSERVICE_MONTHLY_EXPENSES');
			break;
			case "ELECTRICITY BILL":
				$txt = JText::_('OWNSERVICE_ELECTRICITY_BILL');
			break;
			case "WATER BILL":
				$txt = JText::_('OWNSERVICE_WATER_BILL');
			break;
			case "RENT":
				$txt = JText::_('OWNSERVICE_RENT');
			break;
			case "SHOPPING":
				$txt = JText::_('OWNSERVICE_SHOPPING');
			break;
			case "Personal Savings":
				$txt = JText::_('OWNSERVICE_PERSONAL_SAVINGS');
			break;
			case "AIR FARE":
				$txt = JText::_('OWNSERVICE_AIR_FARE');
			break;
			case "TRAVEL":
				$txt = JText::_('OWNSERVICE_TRAVEL');
			break;
			case "MEDICAL":
				$txt = JText::_('OWNSERVICE_MEDICAL');
			break;
			case "EDUCATION":
				$txt = JText::_('OWNSERVICE_EDUCATION');
			break;
			case "CAR INSTALLMENT":
				$txt = JText::_('OWNSERVICE_CAR_INSTALLMENT');
			break;
			case "CAR LOAN REPAYMENT":
				$txt = JText::_('OWNSERVICE_CAR_LOAN_REPAYMENT');
			break;
			case "HOUSE INTALLMENT":
				$txt = JText::_('OWNSERVICE_HOUSE_INTALLMENT');
			break;
			case "HOME LOAN REPAYMENT":
				$txt = JText::_('OWNSERVICE_HOME_LOAN_REPAYMENT');
			break;
			default:
				$txt = $row->reason_other;
			break;
		endswitch;
		
		return ucwords(strtolower($txt));	
	}
	
	function getParams($title = null, $default = null) {

		// Intailize variable
		$db =& JFactory::getDBO();
		$wheres = array();
		
		if ($title == null) {
			$select	= '*';
			$where	= '';
		} else {
			$select		= 'value';
			$wheres[]	= 'title =\''.$title.'\'';
			
			$where = " WHERE " . implode( " AND ", $wheres );
		}
		
		
		$query = ' SELECT '.$select
			. ' FROM #__ownservice_settings '
			. $where
			. ' ORDER BY id';
			
		$db->setQuery($query);
		$settings = $db->loadObjectList();
		
		// All ITEMS
		if ($title == '') {
			return $settings;
		} else {
			// ONLY ONE ITEM
			if (empty($settings)) {
				return $default;
			} else {
				if (isset($settings[0]->value)) {
					return($settings[0]->value);
				} else {
					return '';
				}
			}
		}//End IF
	}

	/**
	 * Checks if the file is an image
	 * @param string The filename
	 * @return boolean
	 */
	function isImage( $fileName ){
		static $imageTypes = 'xcf|odg|gif|jpg|png|bmp';
		return preg_match("/$imageTypes/i",$fileName);
	}

	/**
	 * Checks if the file can be uploaded
	 *
	 * @param array File information
	 * @param string An error message to be returned
	 * @return boolean
	 */
	function canUpload( $file, $i=0, &$err )
	{
		$params = &JComponentHelper::getParams( 'com_media' );

		if(empty($file['name'][$i])) {
			$err = 'Please input a file for upload';
			return false;
		}

		jimport('joomla.filesystem.file');
		if ($file['name'][$i] !== JFile::makesafe($file['name'][$i])) {
			$err = 'WARNFILENAME';
			return false;
		}

		$format = strtolower(JFile::getExt($file['name'][$i]));

		$allowable = explode( ',', $params->get( 'upload_extensions' ));
		$ignored = explode(',', $params->get( 'ignore_extensions' ));
		if (!in_array($format, $allowable) && !in_array($format,$ignored))
		{
			$err = 'WARNFILETYPE';
			return false;
		}

		$maxSize = (int) $params->get( 'upload_maxsize', 0 );
		if ($maxSize > 0 && (int) $file['size'][$i] > $maxSize)
		{
			$err = 'WARNFILETOOLARGE';
			return false;
		}
		
		$user = JFactory::getUser();
		$imginfo = null;
		if($params->get('restrict_uploads',1) ) {
			$images = explode( ',', $params->get( 'image_extensions' ));
			if(in_array($format, $images)) { // if its an image run it through getimagesize
				if(($imginfo = getimagesize($file['tmp_name'][$i])) === FALSE) {
					$err = 'WARNINVALIDIMG';
					return false;
				}
			} else if(!in_array($format, $ignored)) {
				// if its not an image...and we're not ignoring it
				$allowed_mime = explode(',', $params->get('upload_mime'));
				$illegal_mime = explode(',', $params->get('upload_mime_illegal'));
				if(function_exists('finfo_open') && $params->get('check_mime',1)) {
					// We have fileinfo
					$finfo = finfo_open(FILEINFO_MIME);
					$type = finfo_file($finfo, $file['tmp_name'][$i]);
					if(strlen($type) && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) {
						$err = 'WARNINVALIDMIME';
						return false;
					}
					finfo_close($finfo);
				} else if(function_exists('mime_content_type') && $params->get('check_mime',1)) {
					// we have mime magic
					$type = mime_content_type($file['tmp_name'][$i]);
					if(strlen($type) && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) {
						$err = 'WARNINVALIDMIME';
						return false;
					}
				} /*else if(!$user->authorize( 'login', 'administrator' )) {
					$err = 'WARNNOTADMIN';
					return false;
				}*/
			}
		}

		$xss_check =  JFile::read($file['tmp_name'][$i],false,256);
		$html_tags = array('abbr','acronym','address','applet','area','audioscope','base','basefont','bdo','bgsound','big','blackface','blink','blockquote','body','bq','br','button','caption','center','cite','code','col','colgroup','comment','custom','dd','del','dfn','dir','div','dl','dt','em','embed','fieldset','fn','font','form','frame','frameset','h1','h2','h3','h4','h5','h6','head','hr','html','iframe','ilayer','img','input','ins','isindex','keygen','kbd','label','layer','legend','li','limittext','link','listing','map','marquee','menu','meta','multicol','nobr','noembed','noframes','noscript','nosmartquotes','object','ol','optgroup','option','param','plaintext','pre','rt','ruby','s','samp','script','select','server','shadow','sidebar','small','spacer','span','strike','strong','style','sub','sup','table','tbody','td','textarea','tfoot','th','thead','title','tr','tt','ul','var','wbr','xml','xmp','!DOCTYPE', '!--');
		foreach($html_tags as $tag) {
			// A tag is '<tagname ', so we need to add < and a space or '<tagname>'
			if(stristr($xss_check, '<'.$tag.' ') || stristr($xss_check, '<'.$tag.'>')) {
				$err = 'WARNIEXSS';
				return false;
			}
		}
		return true;
	}

	function imageResize($width, $height, $target) {
		//takes the larger size of the width and height and applies the
		//formula accordingly...this is so this script will work
		//dynamically with any size image
		if ($width > $height) {
			$percentage = ($target / $width);
		} else {
			$percentage = ($target / $height);
		}

		//gets the new value and applies the percentage, then rounds the value
		$width = round($width * $percentage);
		$height = round($height * $percentage);

		return array($width, $height);
	}

	function addIcon( $file ){
		$iconfile = '';
		switch ($file->ext) {
			// Image
			case 'jpg':
			case 'png':
			case 'gif':
			case 'xcf':
			case 'odg':
			case 'bmp':
			case 'jpeg':
				$iconfile = JURI::root(true).'/components/com_ownservice/assets/images/mime-icon-16/image.png';
			break;

			// Non-image document
			default:
				$icon = JPATH_SITE.DS.'components'.DS.'com_ownservice'.DS.'assets'.DS.'images'.DS.'mime-icon-16'.DS.$file->ext.'.png';
				if (file_exists($icon)) {
					$iconfile = 'components/com_ownservice/assets/images/mime-icon-16/'.$file->ext.'.png';
				} else {
					$iconfile = 'components/com_ownservice/assets/images/mime-icon-16/unknown.png';
				}
			break;
		}
		return $iconfile;
	}

	/**
	* Retrieves the level for a user
	* @since 1.0
	* @param id  unique id of the user
	* @return string
	*/
	public function getUserLevel($id)
	{
		$db = JFactory::getDBO();

		//If not user id return
		if(!$id)
		{
			return;
		}

		// Get total amount of transferred
		$query = ' SELECT SUM(amount) FROM #__ownservice_transactions'
				.' WHERE state=4'
				.' AND user_id='.(int) $id
				;
		$db->setQuery($query);
		//echo $db->_sql;
		$this->total_amount = $db->loadResult();

		// Get level from amount range of transferred
		$query = ' SELECT id, title, `from`, `to` FROM #__ownservice_levels'
				.' WHERE (`from` <='.$this->total_amount.' AND `to` >='.$this->total_amount.')'
				;
		$db->setQuery($query);
		//echo $db->_sql;
		$level = $db->loadObject();

		//Set Level Id
		$this->level_id = isset($level->id)?$level->id:0;

		$levelto = isset($level->to)?$level->to:0.00;

		// Get next level
		$query 	= ' SELECT `from` FROM #__ownservice_levels'
				 .' WHERE `from`>'.$levelto
				 .' ORDER BY `from` ASC'
				;
		$db->setQuery($query);
		$total_nextamount = $db->loadResult();

		//set amount next level 
		$this->total_nextamount = $total_nextamount?$total_nextamount:0.00;

		if($levelto <= 0.00)
		{
			return 'Member';
		}

		return $level->title;
	}

	/**
	* Get total amount transferred
	*
	* @since 1.0
	* @param 
	* @return flot
	*/
	public function getTotalAmount()
	{
		return $this->total_amount;
	}

	/**
	* Get amount for next level
	*
	* @since 1.0
	* @param 
	* @return flot
	*/
	public function getUplevel()
	{
		return ($this->total_nextamount - $this->total_amount);
	}

	/**
	* Get percent 
	* 
	*/
	public function getPercentLevel()
	{
		//echo 'amount = '.$this->total_amount."<br />";
		//echo 'next level amount = '.($this->total_nextamount)."<br />";
		//echo 'balance ='.($this->total_nextamount-$this->total_amount)."<br />";

		//return ($this->total_amount * 100) / $this->total_nextamount;
		return round( (($this->total_amount * 100) / $this->total_nextamount));
	}

	/**
	* Calcurate update points
	*/
	function updatePoint($id, $userId, $condition)
	{
		$db = JFactory::getDBO();

		//Get user level
		$level = OwnserviceHelper::getUserLevel($userId);

		$query = 'SELECT * FROM #__ownservice_points '
				.' WHERE level_id='.(int) $this->level_id
				;
		$db->setQuery($query);
		$row = $db->loadObject();

		if($row)
		{
			$point = floor( $condition / $row->condition ) * $row->pointearn;

			$objs =& JTable::getInstance('ownservice_memberpoint', '');

			$objs->user_id 			= $userId;
			$objs->transfer_id 		= $id;
			$objs->point 			= $point;
			$objs->method 			= 'Transfer';
			$objs->date 			= date('Y-m-d H:i:s');

			$objs->store();
		}

		return;
	}

	/**
	* Retrive the point of a member
	*/
	function getPoint($userId)
	{
		$db 	= JFactory::getDBO();
		$query 	= " SELECT SUM(point) FROM #__ownservice_member_points "
				.' WHERE user_id='.(int) $userId
				;
		$db->setQuery($query);
		$point = $db->loadResult();

		$point = $point?$point:0;

		return $point;
	}


	/**
	 * Method to build the list levels
	 * 
	 * @return array
	 * @since 1.5
	*/
	function buildlevelsselect($name, $selected, $top = false, $class = 'class="inputbox"'){
		
		$options = array();
		
		if($top) {
			$options[] 	= JHTML::_( 'select.option', '', JText::_( 'OWNSERVICE_SELECT_LEVEL' ) );
		}

		$db 	= JFactory::getDBO();
		$query 	= 'SELECT * FROM #__ownservice_levels';
		$db->setQuery($query);
		$rows 	= $db->loadObjectList();

		if($rows)
		{
			foreach ($rows as $row) {
				$options[] = JHTML::_( 'select.option', $row->id, $row->title);
			}
		}
		return JHTML::_('select.genericlist', $options, $name, $class, 'value', 'text', $selected );
	}
}